<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CategoryModel extends CI_Model{

    function load($page=0,$pagesize=PAGE_SIZE){
        $this->db->select('c1.category_id,c1.category_name,c1.category_order,c1.category_display_state,c2.category_name as category_parent_name');
        $this->db->from('tb_categories as c1');
        $this->db->join('tb_categories as c2','c1.category_parent_id=c2.category_id','left');
        $this->db->where('c1.category_is_delete',0);
        $this->db->order_by('c1.category_order');
        $this->db->limit($pagesize,$page);
        $query = $this->db->get();
        $this->db->select('c1.category_id');
        $this->db->from('tb_categories as c1');
        $this->db->where('c1.category_is_delete',0);
        $total = $this->db->count_all_results();
        $data = array(
            'list' => $query->result(),
            'total' => $total
        );
        return $data;
    }
    function load_cate(){
        $this->db->select('category_id,category_name,category_order,category_display_state,category_parent_id');
        $this->db->where('category_is_delete',0);
        $this->db->order_by('category_order');
        $query = $this->db->get(tbl_category);
        return $query->result();
    }

    function load_cate_id($id){
        $this->db->select('c1.category_id,c1.category_name,c1.category_order,c1.category_display_state,c2.category_name as category_parent_name');
        $this->db->from('tb_categories as c1');
        $this->db->join('tb_categories as c2','c1.category_parent_id=c2.category_id','left');
        $this->db->where('c1.category_is_delete',0);
        $this->db->where('c1.category_id',$id);
        $this->db->order_by('c1.category_order');
        $query = $this->db->get();
        return $query->result();
    }
    function add($info){
        return $this->db->insert(tbl_category,$info);
    }
    function edit($info,$id){
        $where = array('category_id' => $id);
        return $this->db->update(tbl_category,$info,$where);
    }
    function remove($info,$id){
        $this->db->where('category_id',$id);
        return $this->db->update(tbl_category,$info);
    }
}